package com.example.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.utils.L;
import com.example.view.R;
import com.example.view.bean.CityBean;
import com.example.view.data.CityDao;

import java.util.List;

public class CityAdapter extends RecyclerView.Adapter<CityAdapter.CityViewHolder> {

    private final CityDao dao;
    private final List<CityBean> listData;

    public CityAdapter() {
        this.dao = new CityDao();
        this.listData = dao.getRegion(null);
    }


    @NonNull
    @Override
    public CityViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city, parent, false);
        return new CityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CityViewHolder holder, int position) {
        holder.setData(listData.get(position));
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    class CityViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView iv_arrow;
        private TextView tv_name;
        private CityBean data;

        public CityViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_arrow = itemView.findViewById(R.id.iv_arrow);
            tv_name = itemView.findViewById(R.id.tv_name);
            itemView.setOnClickListener(this);
        }

        public void setData(CityBean cityBean) {
            this.data = cityBean;
            itemView.scrollTo(-100 * data.getLevel(), 0);
            iv_arrow.setRotation(data.isOpen() ? 90 : 0);
            tv_name.setText(data.getName());
        }

        @Override
        public void onClick(View v) {
            L.e("position==" + getLayoutPosition());
            if (v == itemView) {
                if (data.isOpen()) {
                    int i = getLayoutPosition() + 1;
                    int count = 0;
                    while (i < listData.size()) {
                        if (listData.get(i).getLevel() > data.getLevel()) {
                            count++;
                            listData.remove(i);
                        } else {
                            break;
                        }
                    }
                    notifyItemRangeRemoved(i, count);
                } else {
                    List<CityBean> all = dao.getRegion(data.getId());
                    for (CityBean bean : all) {
                        bean.setLevel(data.getLevel() + 1);
                    }
                    listData.addAll(getLayoutPosition() + 1, all);
                    notifyItemRangeInserted(getLayoutPosition() + 1, all.size());
                }
                data.setOpen(!data.isOpen());
                notifyItemChanged(getLayoutPosition());
            }
        }

    }
}
